package com.car.service.controller;

import com.car.service.dto.request.PageRequest;
import com.car.service.dto.request.SessionRequest;
import com.car.service.dto.response.PageResponse;
import com.car.service.dto.response.Result;
import com.car.service.entity.ChatSession;
import com.car.service.service.ChatSessionService;
import com.car.service.vo.ChatSessionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 聊天会话控制器
 */
@RestController
@RequestMapping("/api/chat/session")
public class ChatSessionController {
    
    @Autowired
    private ChatSessionService chatSessionService;
    
    /**
     * 创建会话
     */
    @PostMapping
    public Result<ChatSessionVO> createSession(@Valid @RequestBody SessionRequest request) {
        ChatSession session = chatSessionService.createSession(request.getUserId(), request.getServiceId());
        return Result.success(ChatSessionVO.fromEntity(session));
    }
    
    /**
     * 获取会话详情
     */
    @GetMapping("/{sessionId}")
    public Result<ChatSessionVO> getSession(@PathVariable String sessionId) {
        ChatSession session = chatSessionService.getSession(sessionId);
        if (session == null) {
            return Result.error("会话不存在");
        }
        return Result.success(ChatSessionVO.fromEntity(session));
    }
    
    /**
     * 获取用户的会话列表
     */
    @GetMapping("/user/{userId}")
    public Result<PageResponse<ChatSessionVO>> getUserSessions(
            @PathVariable Long userId,
            @ModelAttribute PageRequest pageRequest) {
        List<ChatSession> sessions = chatSessionService.getUserSessions(
                userId, pageRequest.getPage(), pageRequest.getSize());
        
        List<ChatSessionVO> voList = sessions.stream()
                .map(ChatSessionVO::fromEntity)
                .collect(Collectors.toList());
        
        // 这里简化处理，实际应该查询总数
        long total = voList.size();
        
        return Result.success(
                PageResponse.of(voList, total, pageRequest.getPage(), pageRequest.getSize()));
    }
    
    /**
     * 获取客服的会话列表
     */
    @GetMapping("/service/{serviceId}")
    public Result<PageResponse<ChatSessionVO>> getServiceSessions(
            @PathVariable Long serviceId,
            @ModelAttribute PageRequest pageRequest) {
        List<ChatSession> sessions = chatSessionService.getServiceSessions(
                serviceId, pageRequest.getPage(), pageRequest.getSize());
        
        List<ChatSessionVO> voList = sessions.stream()
                .map(ChatSessionVO::fromEntity)
                .collect(Collectors.toList());
        
        // 这里简化处理，实际应该查询总数
        long total = voList.size();
        
        return Result.success(
                PageResponse.of(voList, total, pageRequest.getPage(), pageRequest.getSize()));
    }
    
    /**
     * 关闭会话
     */
    @PutMapping("/{sessionId}/close")
    public Result<Void> closeSession(@PathVariable String sessionId) {
        chatSessionService.closeSession(sessionId);
        return Result.success();
    }
    
    /**
     * 删除会话
     */
    @DeleteMapping("/{sessionId}")
    public Result<Void> deleteSession(@PathVariable String sessionId) {
        chatSessionService.deleteSession(sessionId);
        return Result.success();
    }
} 